package com.ymatou.iapi.tradingsettlement.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymatou.iapi.tradingsettlement.parameter.CommissionOverviewReq;
import com.ymatou.iapi.tradingsettlement.service.CommissionOverviewCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.HunterCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceHunterOrderCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.hunter.HunterSettlementWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

@Ignore
public class Ts_CommissionOverview {

	private static CommissionOverviewReq commissionOverviewReq;
	private static CommissionOverviewCall commissionOverviewCall;

	private static long userId = Long.valueOf(EnvSetup.getData("BuyUserId"));
	HunterSettlementWrapper hunterSettlementWrapper = new HunterSettlementWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("推手销售佣金收益概览 ");
	}

	@Before
	public void caseUp() {
		commissionOverviewReq = new CommissionOverviewReq();
		commissionOverviewCall = new CommissionOverviewCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(commissionOverviewCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyDb() throws JSONException {

		JSONObject data = commissionOverviewCall.getData();

		String actualTodayIncome = hunterSettlementWrapper
				.sumTodayIncome(commissionOverviewReq.getHunterId())
				.stripTrailingZeros().toPlainString();
		String actualTodaySalesAmount = hunterSettlementWrapper
				.sumTodaySalesAmount(commissionOverviewReq.getHunterId())
				.stripTrailingZeros().toPlainString();
		String actualUnSettledAmount = hunterSettlementWrapper
				.sumUnSettledAmount(commissionOverviewReq.getHunterId())
				.stripTrailingZeros().toPlainString();
		String actualCurrentMonthIncome = hunterSettlementWrapper
				.sumCurrentMonthIncome(commissionOverviewReq.getHunterId())
				.stripTrailingZeros().toPlainString();

		DataUtil.verifyEquals(new BigDecimal(data.optString("todayIncome"))
				.stripTrailingZeros().toPlainString(), actualTodayIncome,
				"验证todayIncome");
		DataUtil.verifyEquals(
				new BigDecimal(data.optString("todaySalesAmount"))
						.stripTrailingZeros().toPlainString(),
				actualTodaySalesAmount, "验证todaySalesAmount");
		DataUtil.verifyEquals(new BigDecimal(data.optString("unSettledAmount"))
				.stripTrailingZeros().toPlainString(), actualUnSettledAmount,
				"验证unSettledAmount");
		DataUtil.verifyEquals(
				new BigDecimal(data.optString("currentMonthIncome"))
						.stripTrailingZeros().toPlainString(),
				actualCurrentMonthIncome, "验证currentMonthIncome");

	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CommissionOverview_001() {
		Logger.start(true, "推手销售佣金收益概览(包括当天生成的大礼包数据)， 未结算");
		try {

			Logger.comment("前置数据准备");

			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(userId);

			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
				}
			}

			// 生成一个新的推手
			Long serviverId = HunterCallService.buildHunterRelation(2, 1, 0)
					.get(0);

			// 推手为servicer
			TradingServiceResp serviverPlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(serviverId, userId, 1);

			PayGateWayCallService
					.notifyCall(
							"13",
							TradingCallService.payOrder(userId,
									BigDecimal.ZERO,
									serviverPlaceOrder.getTradingId(), "13")
									.toString(),
							serviverPlaceOrder.getPayableAmount()
									.subtract(BigDecimal.ZERO).toString(),
							(int) userId);
			TradingSyncService.getSyncResult(serviverPlaceOrder.getOrderList()
					.get(0).getOrderId());

			Logger.comment("前置数据结束");

			commissionOverviewReq.setHunterId(serviverId);

			commissionOverviewCall.setData(commissionOverviewReq);
			commissionOverviewCall.callService();

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CommissionOverview_002() {
		Logger.start(true, "推手销售佣金收益概览(包括当天生成的大礼包数据)， 已经结算");
		try {

			Logger.comment("前置数据准备");

			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(userId);

			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
				}
			}

			// 生成一个新的推手
			Long serviverId = HunterCallService.buildHunterRelation(2, 1, 0)
					.get(0);

			// 推手为servicer
			TradingServiceResp serviverPlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(serviverId, userId, 1);
			long sellerId = serviverPlaceOrder.getOrderList().get(0)
					.getSellerId();
			long orderId = serviverPlaceOrder.getOrderList().get(0)
					.getOrderId();

			PayGateWayCallService
					.notifyCall(
							"13",
							TradingCallService.payOrder(userId,
									BigDecimal.ZERO,
									serviverPlaceOrder.getTradingId(), "13")
									.toString(),
							serviverPlaceOrder.getPayableAmount()
									.subtract(BigDecimal.ZERO).toString(),
							(int) userId);
			TradingSyncService.getSyncResult(serviverPlaceOrder.getOrderList()
					.get(0).getOrderId());

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			// 推手结算
			hunterSettlementWrapper.updateExpectSettlementTimeByOrderId(
					YMTDateUtil.getBeforeOrNextDate(-1), orderId);
			HunterCallService.doCommissionSettlement();

			Logger.comment("前置数据结束");

			commissionOverviewReq.setHunterId(serviverId);

			commissionOverviewCall.setData(commissionOverviewReq);
			commissionOverviewCall.callService();

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CommissionOverview_003() {
		Logger.start(true, "推手销售佣金收益概览(包括当天生成的普通商品数据)， 未结算");
		try {

			Logger.comment("前置数据准备");

			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(userId);

			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
				}
			}

			// 生成一个新的推手
			Long serviverId = HunterCallService.buildHunterRelation(2, 1, 0)
					.get(0);

			// 推手为servicer
			TradingServiceResp serviverPlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(serviverId, userId, 2);

			PayGateWayCallService
					.notifyCall(
							"13",
							TradingCallService.payOrder(userId,
									BigDecimal.ZERO,
									serviverPlaceOrder.getTradingId(), "13")
									.toString(),
							serviverPlaceOrder.getPayableAmount()
									.subtract(BigDecimal.ZERO).toString(),
							(int) userId);
			TradingSyncService.getSyncResult(serviverPlaceOrder.getOrderList()
					.get(0).getOrderId());

			Logger.comment("前置数据结束");

			commissionOverviewReq.setHunterId(serviverId);

			commissionOverviewCall.setData(commissionOverviewReq);
			commissionOverviewCall.callService();

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CommissionOverview_004() {
		Logger.start(true, "推手销售佣金收益概览(包括当天生成的普通商品数据)， 已经结算");
		try {

			Logger.comment("前置数据准备");

			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(userId);

			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
				}
			}

			// 生成一个新的推手
			Long serviverId = HunterCallService.buildHunterRelation(2, 1, 0)
					.get(0);

			// 推手为servicer
			TradingServiceResp serviverPlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(serviverId, userId, 2);
			long sellerId = serviverPlaceOrder.getOrderList().get(0)
					.getSellerId();
			long orderId = serviverPlaceOrder.getOrderList().get(0)
					.getOrderId();

			PayGateWayCallService
					.notifyCall(
							"13",
							TradingCallService.payOrder(userId,
									BigDecimal.ZERO,
									serviverPlaceOrder.getTradingId(), "13")
									.toString(),
							serviverPlaceOrder.getPayableAmount()
									.subtract(BigDecimal.ZERO).toString(),
							(int) userId);
			TradingSyncService.getSyncResult(serviverPlaceOrder.getOrderList()
					.get(0).getOrderId());

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			// 推手结算
			hunterSettlementWrapper.updateExpectSettlementTimeByOrderId(
					YMTDateUtil.getBeforeOrNextDate(-1), orderId);
			HunterCallService.doCommissionSettlement();

			Logger.comment("前置数据结束");

			commissionOverviewReq.setHunterId(serviverId);

			commissionOverviewCall.setData(commissionOverviewReq);
			commissionOverviewCall.callService();

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CommissionOverview_005() {
		Logger.start(true, "推手销售佣金收益概览， 只有管理费收益，接口数据应该返回为0");
		try {

			Logger.comment("前置数据准备");

			OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
			List<OrderPo> orderList = orderShardingWapper
					.selectHunterGiftBagOrderbyUserId(userId);

			if (orderList.size() > 0) {
				for (OrderPo po : orderList) {
					TradingCallService.cancelOrder(po.getOrderId(),
							CancelOrderTypeEnum.BUYER_CANCEL_ORDER, userId);
				}
			}

			// 生成一个新合伙人
			Long partnerId = HunterCallService.buildHunterRelation(3, 3, 0)
					.get(0);

			// 生成一个新的服务商
			Long serviverId = HunterCallService.buildHunterRelation(2, 2,
					partnerId).get(0);

			// 生成一个新的推手
			Long hunterId = HunterCallService.buildHunterRelation(1, 2,
					serviverId).get(0);

			// 购买一个普通商品
			TradingServiceResp productPlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 2);
			long sellerId = productPlaceOrder.getOrderList().get(0)
					.getSellerId();
			long orderId = productPlaceOrder.getOrderList().get(0).getOrderId();

			PayGateWayCallService
					.notifyCall(
							"13",
							TradingCallService.payOrder(userId,
									BigDecimal.ZERO,
									productPlaceOrder.getTradingId(), "13")
									.toString(),
							productPlaceOrder.getPayableAmount()
									.subtract(BigDecimal.ZERO).toString(),
							(int) userId);
			TradingSyncService.getSyncResult(productPlaceOrder.getOrderList()
					.get(0).getOrderId());

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			// 推手结算
			hunterSettlementWrapper.updateExpectSettlementTimeByOrderId(
					YMTDateUtil.getBeforeOrNextDate(-1), orderId);
			HunterCallService.doCommissionSettlement();

			// 购买一个大礼包
			TradingServiceResp giftPackagePlaceOrder = TradingPlaceHunterOrderCallService
					.placeHunterOrder(hunterId, userId, 1);

			PayGateWayCallService.notifyCall(
					"13",
					TradingCallService.payOrder(userId, BigDecimal.ZERO,
							giftPackagePlaceOrder.getTradingId(), "13")
							.toString(), giftPackagePlaceOrder
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId);
			TradingSyncService.getSyncResult(productPlaceOrder.getOrderList()
					.get(0).getOrderId());

			Logger.comment("前置数据结束");

			commissionOverviewReq.setHunterId(partnerId);

			commissionOverviewCall.setData(commissionOverviewReq);
			commissionOverviewCall.callService();

			DataUtil.verifyEquals(new BigDecimal(commissionOverviewCall
					.getData().optString("todayIncome")).stripTrailingZeros()
					.toPlainString(), 0, "验证todayIncome");
			DataUtil.verifyEquals(new BigDecimal(commissionOverviewCall
					.getData().optString("todaySalesAmount"))
					.stripTrailingZeros().toPlainString(), 0,
					"验证todaySalesAmount");
			DataUtil.verifyEquals(new BigDecimal(commissionOverviewCall
					.getData().optString("unSettledAmount"))
					.stripTrailingZeros().toPlainString(), 0,
					"验证unSettledAmount");
			DataUtil.verifyEquals(new BigDecimal(commissionOverviewCall
					.getData().optString("currentMonthIncome"))
					.stripTrailingZeros().toPlainString(), 0,
					"验证currentMonthIncome");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CommissionOverview_006() {
		Logger.start(true, "不输入hunterId");
		try {

			commissionOverviewReq.setHunterId(0);

			commissionOverviewCall.setData(commissionOverviewReq);
			commissionOverviewCall.callService();

			DataUtil.verifyEquals(400, commissionOverviewCall.getJSONData()
					.getInt("code"), "验证code");
			DataUtil.verifyEquals(100, commissionOverviewCall.getJSONData()
					.getInt("bcode"), "验证bcode");
			DataUtil.verifyEquals("hunterId:推手id不能为空", commissionOverviewCall
					.getJSONData().getString("msg"), "验证msg");
			DataUtil.verifyEquals(false, commissionOverviewCall.getData()
					.getBoolean("success"), "验证success");
			DataUtil.verifyEquals("ILLEGAL_ARGUMENT", commissionOverviewCall
					.getData().getString("errorCode"),
					"验证errorCode");
			DataUtil.verifyEquals("hunterId:推手id不能为空", commissionOverviewCall
					.getData().getString("errorMessage"),
					"验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
